#pragma once
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>

#include "Repaired.h"
#include "VehicleException.h"

using namespace std;

class Vehicle
{
protected:
	string brand;
	int year;
	double purchasingPrice;
	double sellingPrice;
	vector<Repaired> rep;
public:

	Vehicle()
	{
		brand = "N/A";
		year = -1;
		purchasingPrice = sellingPrice = 0.0;
	}

	Vehicle(string brand, int year, double purchasingPrice, double sellingPrice)
	{
		if (year > 2014 || year < 1700 )
		{
			ostringstream oss;
			oss << "Invalid year " << year << endl << "Valid range is [1700,2014]";
			throw VehicleException(oss.str());
		}
		this->brand = brand;
		this->year = year;
		this->purchasingPrice = purchasingPrice;
		this->sellingPrice = sellingPrice;
	}

	~Vehicle(){}

	void setBrand(string brand){ this->brand = brand; }
	void setYear(int year){ this->year = year; }
	void setPurchasingPrice(double purchasingPrice){ this->purchasingPrice = purchasingPrice; }
	void setSellingPrice(double sellingPrice){ this->sellingPrice = sellingPrice; }

	string getBrand(){ return brand; }
	int getYear(){ return year; }
	double getPurchasingPrice(){ return purchasingPrice; }
	double getSellingPrice(){ return sellingPrice; }

	void write()
	{
		cout << brand << ", " << year << ": " << "Purchase: " << purchasingPrice << ", Sell: " << sellingPrice << endl;
	}

	void read()
	{
		string str;
		cout << "Brand: ";
		getline(cin, brand);
		cout << "Year: ";
		getline(cin, str);
		{
			istringstream iss(str);
			iss >> year;
		}
		cout << "Purchasing price: ";
		getline(cin, str);
		{
			istringstream iss(str);
			iss >> purchasingPrice;
		}
		cout << "Selling price: ";
		getline(cin, str);
		{
			istringstream iss(str);
			iss >> sellingPrice;
		}
	}
	
	//Format: (brand,year,purchasingPrice,sellingPrice)
	friend ostream& operator<<(ostream &out, const Vehicle &v)
	{
		out << "(";
		out << v.brand << ",";
		out << v.year << ",";
		out << v.purchasingPrice << ",";
		out << v.sellingPrice << ")";
		return out;
	}

	friend istream& operator>>(istream &in, Vehicle &v)
	{
		string str;
		getline(in, str, '(');
		getline(in, v.brand, ',');
		getline(in, str, ',');
		{
			istringstream iss(str);
			iss >> v.year;
		}
		getline(in, str, ',');
		{
			istringstream iss(str);
			iss >> v.purchasingPrice;
		}
		getline(in, str, ')');
		{
			istringstream iss(str);
			iss >> v.sellingPrice;
		}
		return in;
	}

	void addRepairs(Repaired r)
	{
		rep.push_back(r);
	}

	virtual void print()
	{
		cout << brand << ", " << year << ": " << "Purchase: " << purchasingPrice << ", Sell: " << sellingPrice;
	}
};

